package Page.AboutUs;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import Navigation.NavigationController;

import java.net.URL;

public class ContactUsPage {

    public static Scene getScene() {
        // 创建文本区域用于显示联系信息
        TextArea contactInfoTextArea = new TextArea();
        contactInfoTextArea.setWrapText(true);
        contactInfoTextArea.setPrefHeight(200);
        contactInfoTextArea.setEditable(false);  // 设置为不可编辑
        contactInfoTextArea.setText(getContactInfo());

        // 使用 ScrollPane 包装 TextArea 以实现滚动
        HBox textAreaBox = new HBox(contactInfoTextArea);
        //textAreaBox.setAlignment(Pos.CENTER);
        ScrollPane scrollPane = new ScrollPane(textAreaBox);
        scrollPane.setFitToWidth(true);

        // 创建负责人信息部分
        VBox responsiblePersonsBox = new VBox(10);
        responsiblePersonsBox.setAlignment(Pos.TOP_CENTER);
        responsiblePersonsBox.setPadding(new Insets(20));

        // 添加负责人信息
        HBox row1 = new HBox(20,
                createPersonInfo("史恩僮", "/res/studentPhoto.jpg", "+86-10-11111111", "person1@networksecurity.com"),
                createPersonInfo("杨林帆", "/res/studentPhoto.jpg", "+86-10-22222222", "person2@networksecurity.com")
        );

        HBox row2 = new HBox(20,
                createPersonInfo("宋卓一", "/res/studentPhoto.jpg", "+86-10-33333333", "person3@networksecurity.com"),
                createPersonInfo("杨基勇", "/res/studentPhoto.jpg", "+86-10-44444444", "person4@networksecurity.com")
        );

        HBox row3 = new HBox(20,
                createPersonInfo("张润宁", "/res/studentPhoto.jpg", "+86-10-55555555", "person5@networksecurity.com"),
                createPersonInfo("喻俊", "/res/studentPhoto.jpg", "+86-10-66666666", "person6@networksecurity.com")
        );

        responsiblePersonsBox.getChildren().addAll(row1, row2, row3);

        // 将组件添加到布局
        VBox vbox = new VBox(10, scrollPane, responsiblePersonsBox);
        vbox.setPadding(new Insets(10,160,10,160));
        vbox.setSpacing(10);

        // 创建主布局并设置场景
        BorderPane root = new BorderPane();
        root.setCenter(vbox);

        VBox vboxRoot = new VBox();
        Button button = new Button("返回");
        button.setOnAction(event -> {
            NavigationController.back();
        });
        vboxRoot.getChildren().add(button);
        vboxRoot.getChildren().add(root);

        Scene scene = new Scene(vboxRoot);

        // 监听场景的宽度和高度属性，强制重新布局
        scene.widthProperty().addListener((obs, oldVal, newVal) -> {
            vboxRoot.requestLayout();
        });

        scene.heightProperty().addListener((obs, oldVal, newVal) -> {
            vboxRoot.requestLayout();
        });

        return scene;
    }

    private static VBox createPersonInfo(String name, String photoPath, String phone, String email) {
        VBox personBox = new VBox(5);
        personBox.setAlignment(Pos.CENTER);

        // 加载照片
        ImageView photoView = new ImageView();
        URL imageUrl = ContactUsPage.class.getResource(photoPath);
        if (imageUrl != null) {
            Image image = new Image(imageUrl.toString());
            photoView.setImage(image);
            photoView.setFitHeight(100);
            photoView.setFitWidth(100);
        } else {
            System.out.println("图片未找到: " + photoPath);
        }

        // 创建标签
        Label nameLabel = new Label(name);
        Label phoneLabel = new Label("电话: " + phone);
        Label emailLabel = new Label("邮箱: " + email);

        // 设置样式
        nameLabel.setStyle("-fx-font-size: 16px; -fx-font-weight: bold;");
        phoneLabel.setStyle("-fx-font-size: 14px;");
        emailLabel.setStyle("-fx-font-size: 14px;");

        // 将标签添加到垂直布局
        personBox.getChildren().addAll(photoView, nameLabel, phoneLabel, emailLabel);

        return personBox;
    }

    private static String getContactInfo() {
        return "联系我们：\n\n" +
                "地址：北京市海淀区学院路37号，北京理工大学网络攻防对抗技术研究所\n" +
                "电话：+86-10-12345678\n" +
                "传真：+86-10-87654321\n" +
                "电子邮件：info@networksecurity.com\n\n" +
                "如果您有任何疑问或需要进一步的信息，请随时通过上述联系方式与我们联系。我们期待您的来信或来电，并将尽快回复您。\n\n" +
                "办公时间：周一至周五，上午9:00 - 下午6:00\n" +
                "研究所网址：www.networksecurity.com\n\n" +
                "感谢您对网络攻防对抗技术研究所的关注和支持！";
    }
}
